<script>
import {selectAllDevice,deleteDevice,getOneDevice,update,addDevice} from '@/api/device/device'
import {delRole} from "@/api/role/role";
export default {
  name: "device",
  data(){
    return{
      statusOptions: [],
      deviceAccessModel:[],
      steamModel:[],
      device:{
        deviceList:[],
      },
      deviceQuery:{
        pageSize:10,
        pageNum:1
      },
      total:0,
      dialogoption:{
        title:"设备修改",
        open:false,
        device:undefined,
        isUpdate:false
      },
      form:{
        channelId:undefined,
        name:undefined
      },
      rules:{
        name:{ required: true, message: '设备名不能为空', trigger: 'blur' },
        channelId:{ required: true, message: '设备编号不能为空', trigger: 'blur' },
        deviceAccessModel:{ required: true, message: '设备接入模式不能为空', trigger: 'blur' },
        ip:{ required: true, message: '该项不能不能为空', trigger: 'blur' }

      }


    }
  },
  methods:{
    getList(){
      selectAllDevice(this.deviceQuery).then(resp=>{
        console.log(resp)
        this.device.deviceList = resp.rows
        this.total = resp.total
      })
    },
    resetQuery(){
      this.deviceQuery = {
        pageSize:10,
        pageNum:1
      }
      this.getList()
    },
    handleDelete(row){
      let delIds = []
      delIds.push(row.channelId)
      var param = {'delIds':delIds}
      this.$model.confirm('是否确认删除设备名称为"' + row.name + '"的数据项？').then(function() {
        return deleteDevice(param);
      }).then(() => {
        this.getList();
        this.$model.msgSuccess("删除成功");
      }).catch(() => {});

    },
    handleUpdate(row){

      var param = {'channelId':row.channelId}

      getOneDevice(param).then(resp=>{
        let dev = resp.data
        this.form.deviceId = dev.deviceId
        this.form.channelId = dev.channelId;
        this.form.name = dev.name
        this.form = resp.data
        this.dialogoption.device = dev
        this.dialogoption.open=true;
        this.dialogoption.isUpdate = true;
      })

      // this.dialogoption.open = true;
    },
    submitForm(){
      console.log(this.form)
      this.$refs["form"].validate(valid => {
        if (valid){
          if (this.form.deviceId){
            update(this.form).then(resp=>{
              this.$model.msgSuccess("修改成功")
              this.dialogoption.open = false
              this.getList()
            })
          }else {
            addDevice(this.form).then(resp=>{
              this.$model.msgSuccess("添加成功")
              this.dialogoption.open = false
              this.getList()
            })
          }
        }
      });

    },
    handleAdd(){
      this.form = {};
      // this.form.deviceAccessModel = '0';
      // this.form.streamMode = '1'
      this.dialogoption.title = '设备接入'
      this.dialogoption.open = true;
      this.dialogoption.isUpdate = false;

    }
  },
  created() {
    this.getList();
    this.getDicts("device_js_type").then(response => {
        this.deviceAccessModel = response.data;
    });
    this.getDicts("stream_model").then(response => {
      this.steamModel = response.data;
    });
    this.getDicts("sys_normal_disable").then(response => {
      this.statusOptions = response.data;
    });
  }
}
</script>

<template>
  <!-- 设备管理 -->
    <div id="device-app">
    <!-- 面包屑 -->
    <el-row>
      <el-col :span="23" :offset="1" style="margin-bottom: 30px;margin-top: 20px;">
        <el-breadcrumb separator="/">
          <el-breadcrumb-item :to="{ path: '/device' }" class="bread"><i class="el-icon-location-outline"></i>系统管理</el-breadcrumb-item>
          <el-breadcrumb-item :to="{ path: '/device' }" class="bread">设备管理</el-breadcrumb-item>
        </el-breadcrumb>
      </el-col>
    </el-row>
    <!-- 表单 -->
        <el-row>
          <el-col :span="6" :offset="2">
            <el-button
                class="butter"
                round
                icon="el-icon-plus"
                size="mini"
                @click="handleAdd"
            >新增</el-button>
          </el-col>
          <!--用户数据-->
          <el-col :span="14" :xs="24">
            <el-form :model="deviceQuery" ref="queryForm" size="mini" :inline="true"  label-width="68px" class="formselect">
              <el-form-item label="设备名称" prop="userName">
                <el-input
                    class="inputem"
                    v-model="deviceQuery.name"
                    placeholder="请输入设备名称"
                    clearable
                    style="width: 240px"
                />
              </el-form-item>
              <el-form-item label="设备状态" prop="status">
                <el-select
                    class="inputem"
                    v-model="deviceQuery.onLine"
                    placeholder="请选择设备状态"
                    clearable
                    style="width: 240px">
                  <el-option
                      v-for="dict in statusOptions"
                      :key="dict.dictValue"
                      :label="dict.dictLabel"
                      :value="dict.dictValue"></el-option>
                </el-select>
              </el-form-item>
              <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="getList">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
              </el-form-item>
            </el-form>
          </el-col>
        </el-row>
        <!-- 列表部分 -->
        <el-row type="flex" justify="center">
        <el-col :span="22">
          <div class="mytable">
          <el-table  :data="device.deviceList" class="tabletab" height="500" stripe>
            <el-table-column type="selection" width="50" align="center" />
            <el-table-column label="设备编号" align="center" key="channelId" prop="channelId"  />
            <el-table-column label="设备名称" align="center" key="name" prop="name"  />
            <el-table-column label="ip地址" align="center" key="ip" prop="ip"  />
            <el-table-column label="在线状态" align="center" key="onLine" prop="onLine">
              <template slot-scope="scope">
                <span v-if="scope.row.onLine == 0">
                  <img src="@/assets/ptz/heartbeat.png" alt="">
                  在线
                </span>
                <span v-else>
                  <img src="@/assets/ptz/heartoffline.png" alt="">
                  离线
                </span>
              </template>
            </el-table-column>
            <el-table-column label="注册时间" align="center" key="registerTime" prop="registerTime" >
              <template slot-scope="scope">
                <i class="el-icon-timer" style="color: #1296db;"></i>
                {{parseTime(scope.row.registerTime)}}
              </template>
            </el-table-column>
            <el-table-column label="心跳时间" align="center" key="keepaliveTime" prop="keepaliveTime">
              <template slot-scope="scope">
                <i class="el-icon-timer" style="color: #1296db;"></i>
                {{parseTime(scope.row.keepaliveTime)}}
              </template>
            </el-table-column>
            <el-table-column
                label="操作"
                align="center"
                width="160"
                class-name="small-padding fixed-width"
            >
              <template slot-scope="scope">
                <el-button
                    size="mini"
                    type="text"
                    icon="el-icon-edit"
                    @click="handleUpdate(scope.row)"
                    v-hasPermi="['sys:device:update']"
                >修改</el-button>
                <el-button
                    size="mini"
                    type="text"
                    icon="el-icon-delete"
                    @click="handleDelete(scope.row)"
                    v-hasPermi="['sys:device:del']"
                >删除</el-button>
              </template>
            </el-table-column>
          </el-table>
          </div>
        </el-col>
        </el-row>
        <el-row type="flex" justify="end">
          <el-col :span="10">
            <pagination
              v-show="total>0"
              :total="total"
              :page.sync="deviceQuery.pageNum"
              :limit.sync="deviceQuery.pageSize"
              @pagination="getList"/>
          </el-col>
        </el-row>

      <!-- 修改部分 -->
      <el-dialog :title="dialogoption.title" :visible.sync="dialogoption.open" width="600px" append-to-body>
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-row>
            <el-col :span="12">
              <el-form-item label="接入模式" prop="deviceAccessModel">
                <el-select v-model="form.deviceAccessModel" placeholder="请选择接入模式">
                  <el-option
                      v-for="dict in deviceAccessModel"
                      :key="dict.dictValue"
                      :label="dict.dictLabel"
                      :value="dict.dictValue"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="12">
              <el-form-item label="设备编号" prop="channelId">
                <el-input v-model="form.channelId"  maxlength="30" :disabled="dialogoption.isUpdate" placeholder="请输入设备编号" :title="form.channelId"/>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="设备名称" prop="name">
                <el-input v-model="form.name" placeholder="请输入设备名称" maxlength="30" />
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="24">
              <el-form-item :label="form.deviceAccessModel == 0 ? '视频流' : '设备ip'" prop="ip" >
                <el-input v-model="form.ip"  maxlength="120" :placeholder="form.deviceAccessModel == 0 ? '请输入设备视频流' : '请输入设备Ip'"/>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="拉流模式" prop="streamMode" >
                <el-select v-model="form.streamMode" placeholder="请选择拉流模式">
                  <el-option
                      v-for="dict in steamModel"
                      :key="dict.dictValue"
                      :label="dict.dictLabel"
                      :value="dict.dictValue"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="this.dialogoption.open=false">取 消</el-button>
        </div>
      </el-dialog>
    </div>
</template>

<style scoped>
.bread /deep/ .el-breadcrumb__inner{
  color: #000000;
}
.bread /deep/ .el-breadcrumb__inner:hover{color: #000000;}
.formselect .el-button{
  background-color: #4686D0;
  color: #ffffff;
  border: #4686D0;
  font-size: 12px;
  font-family: "微软雅黑";
}
.butter{
  background-color: #4686D0;
  color: #ffffff;
  border: #4686D0;
  font-size: 12px;
}
.el-row{
  margin-top: 10px;
  /* margin-bottom:20px; */
}
.formselect /deep/ .el-form-item__label{
  /* color: #f2f2f2; */
}
.inputem /deep/ .el-input__inner {
  background-color: rgba(0, 0, 0, 0) ;
  border:1px solid #B1B4C5;
}
.inputem /deep/ .el-input__inner:focus{
  border:2px solid #4c87c2;
}
.inputem /deep/ .el-input__inner:hover{
  border:2px solid #4c87c2;
}

.inputem /deep/ .el-input-group__append{
  background-color: #265389;
  border:1px solid #265389;
}
/*.tabletab /deep/ .el-table__row{*/
/*  background-color: #131627;*/
/*  font-family: "微软雅黑";*/
/*  font-size: 14px;*/
/*}*/
/* */
/*.tabletab /deep/ .el-table__body-wrapper{*/
/*  background-color: #131627;*/
/*  font-family: "微软雅黑";*/
/*  font-size: 14px;*/
/*}*/
/*.tabletab .el-button{*/
/*  color:#ffffff*/
/*}*/

/*::v-deep .cell{*/
/*  color:#ffffff;*/
/*}*/
/*::v-deep.el-table tbody tr:hover > td {*/
/*    background-color: #2b3b5256 !important;*/

/*    }*/
/* .tabletab /deep/ .table_header{*/
/*  background-color: #2b3b52 !important;*/
/*  width: 0;*/
/* }*/
/* .mytable /deep/ .el-table__row>td{*/
/*  !* 去除表格线 *!*/
/*  border-bottom:1px solid #2B3B52;*/
/*}*/
/*.mytable /deep/ .el-table th.is-leaf {*/
/*  !* 去除上边框 *!*/
/*    border: none;*/
/*}*/
/*::v-deep .el-table::before{*/
/*  !* 去除下边框 *!*/
/*  !* height: 0; *!*/
/*  background-color: #2B3B52;*/
/*  !* border:none; *!*/
/*}*/
/*.mytable /deep/ .el-table th.el-table__cell{*/
/*  background-color: #2b3b52;*/
/*}*/
</style>
